package com.china08.yxyapi.api.manage.tec.teaching.lecture.catalog;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.mongodb.DBObject;

/**
 * 老师 ---备授课--备课目录查询
 * 
 */
@CrossOrigin
@RestController
public class LectureBookContentController extends YxyApiBaseController {

    @Autowired
    private MongoTemplate mt;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/catalog/bookcontent", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();
        Criteria criteria = Criteria.where("userId").is(userId).and("schoolId").is(schoolId).and("isDelete").ne(1);
        TypedAggregation<YxyLecturePrePack> aggregation = Aggregation.newAggregation(YxyLecturePrePack.class,
                Aggregation.match(criteria), Aggregation.group("bookcontentsId"));
        AggregationResults<YxyLecturePrePack> result = mt.aggregate(aggregation, YxyLecturePrePack.class);
        RespModel respModel = new RespModel();
        @SuppressWarnings("unchecked")
        List<DBObject> dbObjects = (List<DBObject>) result.getRawResults().get("result");
        dbObjects.stream().forEach(x -> respModel.bookContentId.add((String) x.get("_id")));
        return respModel;
    }

    protected static class RespModel {
        public List<String> bookContentId = new ArrayList<>();
    }
}
